package dao

import (
	"github.com/jinzhu/gorm"
	"reflect"
)

type BaseDao struct {
	gormdb *gorm.DB
}

//model要指针类型
func (dao *BaseDao) Add(model interface{}) (id uint, err error) {
	err = dao.gormdb.Create(model).Error

	obj := reflect.ValueOf(model)
	elem := obj.Elem()

	if elem.Kind() == reflect.Struct {
		tempId := elem.FieldByName("ID")

		if tempId.Kind() == reflect.Uint {

			return uint(tempId.Uint()), err
		}
	}

	return 0, nil
}

//model要指针类型
func (dao *BaseDao) Update(model interface{}) error {
	return dao.gormdb.Save(model).Error
}

//model要指针类型
func (dao *BaseDao) Delete(model interface{}) error {
	return dao.gormdb.Delete(model).Error
}
